//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.live.app.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.live.configuration.SessionInfo;
import com.ruoyi.live.base.domain.Task;
import com.ruoyi.live.base.domain.TaskExpert;
import com.ruoyi.live.base.domain.TaskUserLearnLog;
import com.ruoyi.live.base.domain.TaskUserLog;
import com.ruoyi.live.base.mapper.TaskExpertMapper;
import com.ruoyi.live.base.mapper.TaskMapper;
import com.ruoyi.live.base.mapper.TaskUserLearnLogMapper;
import com.ruoyi.live.base.mapper.TaskUserLogMapper;
import com.ruoyi.live.base.service.ITaskExpertService;
import com.ruoyi.live.tool.commons.ICommons;
import com.ruoyi.live.tool.utils.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import jakarta.servlet.http.HttpServletRequest;
import java.io.File;
import java.math.BigDecimal;
import java.util.*;

@Service
public class RaceService {
    @Autowired
    TaskMapper taskMapper;
    @Autowired
    TaskUserLearnLogMapper taskUserLearnLogMapper;
    @Autowired
    TaskUserLogMapper taskUserLogMapper;
    @Autowired
    TaskExpertMapper taskExpertMapper;
    @Autowired
    ITaskExpertService taskExpertService;

    public RaceService() {
    }

    public List<Task> loadLeftMenu() {
        return this.taskMapper.selectList((Wrapper)(new QueryWrapper(new Task())).orderByAsc("orders"));
    }

    public Long getTaskUserId(Long taskId) {
        TaskUserLearnLog taskUserLearnLog = (TaskUserLearnLog)this.taskUserLearnLogMapper.selectOne(new QueryWrapper((new TaskUserLearnLog()).setTaskId(taskId).setCreateUser(SessionInfo.getUserId())));
        if (taskUserLearnLog == null) {
            Task task = (Task)this.taskMapper.selectById(taskId);
            taskUserLearnLog = (new TaskUserLearnLog()).setTaskId(taskId).setState(1).setCreateUser(SessionInfo.getUserId()).setCreateTime(System.currentTimeMillis()).setClassId(SessionInfo.getClassId()).setCatalogId(task.getCatalogId());
            this.taskUserLearnLogMapper.insert(taskUserLearnLog);
        }

        return taskUserLearnLog.getId();
    }

    public void saveTaskUserData(Long taskUserId, String data) {
        if (!ICommons.isNullOrEmpty(taskUserId) && !ICommons.isNullOrEmpty(data)) {
            TaskUserLearnLog taskUserLearnLog = (TaskUserLearnLog)this.taskUserLearnLogMapper.selectById(taskUserId);
            if (ICommons.isNullOrEmpty(taskUserLearnLog)) {
                throw new BusinessException("参数缺失");
            } else if (!Objects.equals(SessionInfo.getUserId(), taskUserLearnLog.getCreateUser())) {
                throw new BusinessException("不能保存别人的任务");
            } else {
                this.taskUserLogMapper.insert((new TaskUserLog()).setTaskUserId(taskUserId).setData(data).setCreateTime(System.currentTimeMillis()).setCreateUser(SessionInfo.getUserId()));
                this.taskUserLearnLogMapper.updateById((new TaskUserLearnLog()).setId(taskUserId).setTaskUserData(data));
            }
        } else {
            throw new BusinessException("参数缺失");
        }
    }

    public Message getTaskUserData(Long taskUserId) {
        if (ICommons.isNullOrEmpty(taskUserId)) {
            throw new BusinessException("参数缺失");
        } else {
            TaskUserLearnLog taskUserLearnLog = (TaskUserLearnLog)this.taskUserLearnLogMapper.selectById(taskUserId);
            if (ICommons.isNullOrEmpty(taskUserLearnLog)) {
                throw new BusinessException("参数缺失");
            } else if (!Objects.equals(SessionInfo.getUserId(), taskUserLearnLog.getCreateUser())) {
                throw new BusinessException("不能保存别人的任务");
            } else {
                return ICommons.isNullOrEmpty(taskUserLearnLog.getTaskUserData()) ? new Message(false, "") : new Message(true, taskUserLearnLog.getTaskUserData());
            }
        }
    }

    public void uploadRaceFile(Long taskUserId, MultipartFile file, HttpServletRequest request) {
        if (!ICommons.isNullOrEmpty(taskUserId) && !ICommons.isNullOrEmpty(file)) {
            TaskUserLearnLog taskUserLearnLog = (TaskUserLearnLog)this.taskUserLearnLogMapper.selectById(taskUserId);
            if (ICommons.isNullOrEmpty(taskUserLearnLog)) {
                throw new BusinessException("参数缺失");
            } else if (!Objects.equals(SessionInfo.getUserId(), taskUserLearnLog.getCreateUser())) {
                throw new BusinessException("不能保存别人的任务");
            } else {
                try {
                    String fileName = file.getOriginalFilename();
                    if (ICommons.isNullOrEmpty(fileName)) {
                        throw new BusinessException("文件名不能为空");
                    } else {
                        if (fileName.indexOf("\\") != -1) {
                            fileName = fileName.substring(fileName.lastIndexOf("\\"));
                        }

                        String ext = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
                        if (ICommons.isNullOrEmpty(ext)) {
                            throw new BusinessException("扩展名不能为空");
                        } else if (ext.contains("jsp")) {
                            throw new BusinessException("你要干嘛?");
                        } else {
                            String filePath = request.getServletContext().getRealPath("/static/static/data/video/");
                            File targetFile = new File(filePath);
                            if (!targetFile.exists()) {
                                targetFile.mkdirs();
                            }

                            String name = UUID.randomUUID().toString().replaceAll("-", "") + ext;

                            try {
                                File dest = new File(filePath + name);
                                file.transferTo(dest);
                                this.taskUserLogMapper.insert((new TaskUserLog()).setTaskUserId(taskUserId).setData(name).setCreateTime(System.currentTimeMillis()).setCreateUser(SessionInfo.getUserId()));
                                this.taskUserLearnLogMapper.updateById((new TaskUserLearnLog()).setId(taskUserId).setTaskUserData(name));
                            } catch (Exception var11) {
                                var11.printStackTrace();
                                throw new BusinessException("上传失败");
                            }
                        }
                    }
                } catch (Exception var12) {
                    var12.printStackTrace();
                    throw var12;
                }
            }
        } else {
            throw new BusinessException("参数缺失");
        }
    }

    public List<TaskExpert> doLoadDatas() {
        List<Long> ids = this.taskExpertMapper.selectNotIds(SessionInfo.getUser().getUserSchool(), SessionInfo.getUserId());
        List<TaskExpert> taskExperts = new ArrayList();
        Iterator var3 = ids.iterator();

        while(var3.hasNext()) {
            Long userId = (Long)var3.next();
            taskExperts.add((new TaskExpert()).setExpertId(SessionInfo.getUserId()).setState(1).setUserId(userId));
        }

        this.taskExpertService.saveBatch(taskExperts);
        return this.taskExpertMapper.selectExperts(SessionInfo.getUserId());
    }

    public Message setOneScore(TaskExpert expert) {
        if (expert.getId() == null) {
            return new Message(false, "参数不能为空");
        } else {
            this.taskExpertMapper.updateById(expert);
            return new Message(true, "保存成功");
        }
    }

    public TaskExpert loadScores(Long id) {
        return (TaskExpert)this.taskExpertMapper.selectById(id);
    }

    public void finishSubmitJudge(Long id) {
        TaskExpert taskExpert = (TaskExpert)this.taskExpertMapper.selectById(id);
        if (taskExpert == null) {
            throw new BusinessException("获取评分失败");
        } else if (ICommons.isNullOrEmpty(taskExpert.getScore1())) {
            throw new BusinessException("模块1未评分");
        } else if (ICommons.isNullOrEmpty(taskExpert.getScore2())) {
            throw new BusinessException("模块2未评分");
        } else if (ICommons.isNullOrEmpty(taskExpert.getScore3())) {
            throw new BusinessException("模块3未评分");
        } else if (ICommons.isNullOrEmpty(taskExpert.getScore4())) {
            throw new BusinessException("模块4未评分");
        } else if (ICommons.isNullOrEmpty(taskExpert.getScore5())) {
            throw new BusinessException("模块5未评分");
        } else if (ICommons.isNullOrEmpty(taskExpert.getScore6())) {
            throw new BusinessException("模块6未评分");
        } else if (ICommons.isNullOrEmpty(taskExpert.getScore7())) {
            throw new BusinessException("模块7未评分");
        } else {
            BigDecimal sum = taskExpert.getScore1().add(taskExpert.getScore2()).add(taskExpert.getScore3()).add(taskExpert.getScore4()).add(taskExpert.getScore5()).add(taskExpert.getScore6()).add(taskExpert.getScore7());
            taskExpert.setSumScore(sum).setState(2);
            this.taskExpertMapper.updateById(taskExpert);
        }
    }
}
